<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\AdminController;
use Auth;
use Illuminate\Support\Facades\DB;
use Session;
use App\Model\Admin;
use App\Model\Role;

class SetController extends AdminController
{
    
    /**
     * 修改密码
     * @author yhs 2019.07.28
     * @return data
     */
    public function updataPwd(Request $request){
        $model = Auth::guard('admin')->user();//dd(Auth::guard('admin')->user());die;
        //$uid = intval($request->input('uid'));
        //$admin = DB::table('admin')->where('id',$uid)->first();
        $data = [];
        if(bcrypt($request->input('original_password')) != $model->password){
            $data['msg'] = "原密码不正确！";
            $data['status'] = 0;
            return $data;
        }
        if($request->input('password') != $request->input('password_confirmation')){
            $data['msg'] = "新密码和确认密码不一致！";
            $data['status'] = 0;
            return $data;
        }
        DB::beginTransaction();
        try{
            //DB::table('admin')->where('id',$uid)->update(['password' => bcrypt($request->input('password'))]);
            $model->password = bcrypt($request->input('password'));
            $model->save();
            DB::commit();
            $data['msg'] = "密码修改成功！";
            $data['status'] = 1;
            return $data;
        }catch(\Execption $e){
            $data['msg'] = $e->getMessage();
            $data['status'] = 0;
            return $data;
            DB::rollBack();
        }
    }
    /**
     * 平台列表
     * @author yhs 2019.07.28
     * @return list
     */
    public function structure(Request $request){
        $limit = $request->limit;
        $table =DB::table('structure');
        if(!empty($request->seach)){
            //$table->where(['code'=>['like','{{$request->seach}}%']]);
            $table->where('code','like',$request->seach.'%');
            $table->orWhere('channel_name','like',$request->seach.'%');
        }
        $list = $table->paginate($limit);
        return $list;
    }
    /**
     * 修改平台
     * @author yhs 2019.07.28
     * @return array
     */
    public function structureEdit(Request $request){
        if(!empty($request->id)){
            $find = DB::table('structure')->where('id',$request->id)->first();
        }else{
            $find = [];
        }
        return (array)$find;
    }
    /**
     * 添加、修改平台
     * @author yhs 2019.07.28
     * @return msg
     */
    public function structureAdd(Request $request){
        $data = [
            'uid'=>$request->uid,
            'code'=>$request->code,
            'channel_name'=>$request->channel_name,
            'channel_cate'=>$request->channel_cate,
            'desc'=>$request->desc,
            'business_name'=>$request->business_name,
            'company'=>$request->company,
        ];
        try{
            if(empty($request->id)){
                $data['create_time'] = time();
                DB::table('structure')->insert($data);
                $msg['msg'] = "添加成功！";
            }else{
                $data['update_time'] = time();
                DB::table('structure')->where('id',$request->id)->update($data);
                $msg['msg'] = "修改成功！";
            }
            $msg['status'] = 1;
            return $msg;
        }catch(\Execption $e){
            $msg['msg'] = $e->getMessage();
            $msg['status'] = 0;
            return $msg;
        }
    }
    /**
     * 删除平台
     * @author yhs 2019.07.28
     * @return data
     */
    public function structureDel(Request $request){
        if($request->id){
            if($request->status == 0){
                $str = DB::table('structure')->where('id',$request->id)->delete();
            }else{
                $ids = explode(',',$request->id);
                //DB::table('structure')->whereIn('id',$ids);
                $str = DB::table('structure')->whereIn('id',$ids)->delete();
            }
           if($str){
                $data['message'] = "删除成功！";
                $data['status'] = 1;
           }else{
                $data['message'] = "删除失败！";
                $data['status'] = 0;
           }
        }else{
            $data['message'] = "请选择数据！";
            $data['status'] = 0; 
        }
        return $data;
    }
    /**
     * 账号列表
     * @author yhs 2019.07.08.01
     */
    public function getAdmins(Request $request){
        $limit = $request->limit;
        $list = Admin::select('id','username','roleid')->paginate($limit);
        foreach($list as $k=>$v){
            $roleName = Role::select('name')->where('id',$v['roleid'])->first();
            $v['roleid'] = $roleName['name'];
        }
        $role = Role::select('id','name')->get();
        $data = [
            'role' => $role,
            'list' => $list
        ];
        return $data;
    }
    /**
     * 添加、修改账号密码
     * @author yhs 2019.07.08.02
     */
    public function adminsAdd(Request $request){
        if($request->password != $request->confirm_password){
            $msg['message'] = '两次密码不一致！';
            $msg['status'] = 0;
            return $msg;
        }
        $data = [
            'username'=>$request->username,
            'password'=>bcrypt($request->password),
            'roleid'=>$request->roleid,
        ];
        try{
            if(empty($request->id)){
                Admin::insert($data);
                $msg['message'] = "添加成功！";
            }else{
                $data['update_time'] = time();
                Admin::where('id',$request->id)->update($data);
                $msg['message'] = "修改成功！";
            }
            $msg['status'] = 1;
            return $msg;
        }catch(\Execption $e){
            $msg['message'] = $e->getMessage();
            $msg['status'] = 0;
            return $msg;
        }
    }
    /**
     * 修改账页面
     * @author yhs 2019.07.08.02
    */
    public function adminsEdit(Request $request){
        return Admin::select('id','username','roleid')->where('id',$request->id)->first();
    }
    /**
     * 删除账号
     * @author yhs 2019.07.08.02
    */
    public function adminsDelete(Request $request){
        if($request->id){
            $str = Admin::where('id',$request->id)->delete();
           if($str){
                $data['message'] = "删除成功！";
                $data['status'] = 1;
           }else{
                $data['message'] = "删除失败！";
                $data['status'] = 0;
           }
        }else{
            $data['message'] = "请选择数据！";
            $data['status'] = 0; 
        }
        return $data;
    }
}
